﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VehicleMaintenance.Framework;
using VehicleMaintenance.Models;
using VehicleMaintenance.Comm;
using VehicleMaintenance.Events;

namespace VehicleMaintenance.BaseInfo
{
    public partial class GoodsForm : AddOrEditBaseForm
    {
        #region 私有变量及属性
       
        /// <summary>
        /// 记录当前商品ID
        /// </summary>
        private int goodID = 0;

        #endregion 私有变量及属性

        public GoodsForm()
        {
            InitializeComponent();
			ucFormInput1.InitializeControls(typeof(Models.Goods));
            InitializeSepcial();
        }

        public GoodsForm(int id)
        {
            InitializeComponent();
			ucFormInput1.InitializeControls(typeof(Models.Goods));
            InitializeSepcial();

            goodID = id;
            state = State.Edit;
        }

        private void GoodsForm_Load(object sender, EventArgs e)
        {
            try
            {
                if (this.state == State.Edit)
                {
                    CarDBContext db = new CarDBContext();

                    Goods goods = db.Goods.Include("GoodType").Include("CarType").First(p => p.GoodID == goodID);
                    ucFormInput1.SetValues(goods);

                    ucFormInput1.GetControlByName("GoodTypeID").Text = goods.GoodType.Name;
                    ucFormInput1.GetControlByName("CarTypeID").Text = goods.CarType.Name;
                }
                else
                {
                    ucFormInput1.ClearData();
                }
            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                CarDBContext db = new CarDBContext();

                if (!ucFormInput1.VerifyInput())
                {
                    return;
                }

                if (this.state == State.Edit)
                {
                    Goods good = db.Goods.First(p => p.GoodID == goodID);
                    ucFormInput1.GetValues(good);

                    db.SaveChanges();

                    this.ShowInfo("保存成功！");
                }
                else
                {
                    //查询商品编号是否重复
                    string strCode = ucFormInput1.GetControlByName("Code").Text;
                    if (db.Goods.FirstOrDefault(p => p.Code == strCode) != null)
                    {
                        this.ShowInfo("商品编号已经存在");
                        //tabPanel.Controls["Code"]();
                        ucFormInput1.GetControlByName("NO").Focus();
                        return;
                    }
                    var good = db.Goods.Create();

                    ucFormInput1.GetValues(good);

                    db.Goods.Add(good);
                    db.SaveChanges();

                    if (MessageBox.Show(this, "保存成功，是否继续添加？", "提示", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    {
                        ucFormInput1.ClearData();
                        ucFormInput1.GetControlByName("Code").Focus();
                    }
                    else
                    {
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                    }
                }

            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }

        private void InitializeSepcial()
        {
            try
            {
                ucFormInput1.GetControlByName("GoodTypeID").Click += new EventHandler(goodType_Click);
                ucFormInput1.GetControlByName("CarTypeID").Click += new EventHandler(carType_Click);
				ucFormInput1.GetControlByName("Number").KeyPress += new KeyPressEventHandler(KeyEvents.Number_KeyPress);
				ucFormInput1.GetControlByName("Top").KeyPress += new KeyPressEventHandler(KeyEvents.Number_KeyPress);
				ucFormInput1.GetControlByName("Bottom").KeyPress += new KeyPressEventHandler(KeyEvents.Number_KeyPress);
				ucFormInput1.GetControlByName("RetailPrice").KeyPress += new KeyPressEventHandler(decimal_KeyPress);
				ucFormInput1.GetControlByName("WholesalePrice").KeyPress += new KeyPressEventHandler(decimal_KeyPress);
				ucFormInput1.GetControlByName("PurchasePrice").KeyPress += new KeyPressEventHandler(decimal_KeyPress);
            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }

		void decimal_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!(char.IsNumber(e.KeyChar) || e.KeyChar.ToString().Equals(".") || char.IsControl(e.KeyChar)))
			{
				e.Handled = true;
			}
		}

        void goodType_Click(object sender, EventArgs e)
        {
            try
            {
                

                TextBox txt = (sender as TextBox);

                GoodTypeForm form = new GoodTypeForm();
                if (form.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                {
                    txt.Text = form.GoodType.Name;
                    txt.Tag = form.GoodType.GoodTypeID;
                }


            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }

        void carType_Click(object sender, EventArgs e)
        {
            try
            {
                

                TextBox txt = (sender as TextBox);

                CarTypeForm form = new CarTypeForm();
                if (form.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                {
                    txt.Text = form.CarType.Name;
                    txt.Tag = form.CarType.CarTypeID;
                }


            }
            catch (Exception ex)
            {
                this.ShowInfo(ex.Message);
            }
        }       

    }
}
